package com.fernfx.xingtan.my.ui;

import android.accounts.Account;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.common.photo.PhotoActivity;
import com.fernfx.xingtan.main.entity.MyUserInfoEntity;
import com.fernfx.xingtan.my.contract.MySettingContract;
import com.fernfx.xingtan.my.entity.UserInfoEntity;
import com.fernfx.xingtan.my.presenter.MySettingPresenter;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.GlideUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/13.
 * 编辑资料
 */
public class MySettingActivity extends PhotoActivity implements View.OnClickListener, MySettingContract.View {

    @BindView(R.id.user_logo_iv)
    ImageView userLogoIv;
    @BindView(R.id.user_nickname_tv)
    TextView nickNameTv;
    @BindView(R.id.user_sex_tv)
    TextView userSexTv;
    @BindView(R.id.user_area_tv)
    TextView userAreaTv;
    @BindView(R.id.user_account_tv)
    TextView userAccountTv;
    @BindView(R.id.user_calling_card_tv)
    TextView userCallingCardTv;
    @BindView(R.id.user_autograph_tv)
    TextView userAutoGraphTv;
    //presenter
    private MySettingContract.Presenter presenter;
    //昵称
    public final static int NICK_NAME_RESULT = 1;
    public final static String NICK_NAME_TEXT = "nick_name";
    //性别
    public final static int SEX_RESULT = 2;
    public final static String SEX_TEXT = "sex";
    //名片
    public final static int CALLING_CARD_RESULT = 4;
    public final static String CALLING_CARD_TEXT = "calling_card";
    //签名
    public final static int AUTOGRAPH_RESULT = 5;
    public final static String AUTOGRAPH_TEXT = "autograph";
    //image path
    private String imgPath;

    public static void start(Context context) {
        Intent intent = new Intent(context, MySettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_setting;
    }

    @Override
    protected void initView() {
        //init presenter
        presenter = new MySettingPresenter();
        presenter.init(this);
        //设置页面数据
        setToolbarTitle("编辑资料");
        this.imageLogoIv = userLogoIv;
    }

    @Override
    protected void initData() {
        //请求接口获取用户信息
        requestArgsMap.clear();
        presenter.request(requestArgsMap);
        //上传图片成功回调的函数
        uploadSucessCallback = new UploadSucessCallback() {
            @Override
            public void request(String imgPath) {
                requestArgsMap.clear();
                requestArgsMap.put("avatar", imgPath);
                presenter.updateAvatar(requestArgsMap);
            }
        };
    }

    @OnClick({R.id.user_logo_iv, R.id.user_nickname_rlt, R.id.user_sex_rlt, R.id.user_calling_card_rlt, R.id.user_autograph_rlt, R.id.user_area_rlt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_logo_iv:
                presenter.showUserLogoDialog(mContext);
                break;
            case R.id.user_nickname_rlt:
                String nickNameText = trimTextView(nickNameTv);
                ShowSettingInfoActivity.start(this, "设置昵称", NICK_NAME_RESULT, nickNameText);
                break;
            case R.id.user_sex_rlt:
                String sexText = "";
                if (null != userSexTv.getTag()) {
                    sexText = userSexTv.getTag().toString();
                }
                ShowSettingInfoActivity.start(this, "选择性别", SEX_RESULT, sexText);
                break;
            case R.id.user_calling_card_rlt:
                ShowSettingInfoActivity.start(this, "我的二维码", CALLING_CARD_RESULT, "");
                break;
            case R.id.user_autograph_rlt:
                String autoGraphText = trimTextView(userAutoGraphTv);
                ShowSettingInfoActivity.start(this, "个性签名", AUTOGRAPH_RESULT, autoGraphText);
                break;
            case R.id.user_area_rlt:
                //地区
                String areaText = trimTextView(userAreaTv);
                ShowSettingInfoActivity.start(this, "个性签名", AUTOGRAPH_RESULT, areaText);
                break;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

    @Override
    public void showUserInfo(UserInfoEntity userInfoEntity) {
        String avatar = OtherUtil.checkEmptyDefault(userInfoEntity.getObj().getAvatar());
        //如果没有设置过头像就设置默认的头像
        if (TextUtils.isEmpty(avatar)) {
            userLogoIv.setBackgroundResource(R.mipmap.default_user_logo);
        } else {
            GlideUtil.loadCircleImage(mContext, avatar, userLogoIv);
        }
        //昵称
        String nickName = OtherUtil.checkEmptyDefault(userInfoEntity.getObj().getNickName());
        nickNameTv.setText(nickName);
        //性别
        String sexText = userInfoEntity.getObj().getSex();
        if (Constant.ApplicationVariable.SEX_STATUS.equals(sexText)) {
            userSexTv.setText("女");
            userSexTv.setTag(sexText);
        } else {
            userSexTv.setText("男");
            userSexTv.setTag(sexText);
        }
        //地区
        String area = OtherUtil.checkEmptyDefault(userInfoEntity.getObj().getCity());
        userAreaTv.setText(area);
        //账号
        String account = OtherUtil.checkEmptyDefault(String.valueOf(userInfoEntity.getObj().getUserId()));
        userAccountTv.setText(account);
        //签名
        String autoGraph = OtherUtil.checkEmptyDefault(userInfoEntity.getObj().getSignature());
        userAutoGraphTv.setText(autoGraph);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case NICK_NAME_RESULT:
                //更新昵称
                if (data.hasExtra(NICK_NAME_TEXT)) {
                    String nickNameText = data.getStringExtra(NICK_NAME_TEXT);
                    nickNameTv.setText(nickNameText);
                    //更新本地登录保存的昵称
                    if (AccountManager.isLogin()) {
                        MemberEntity memberEntity = AccountManager.getUserInfo();
                        memberEntity.getObj().setNickname(nickNameText);
                        //更新登录内容
                        AccountManager.clearLoginUserInfo();
                        AccountManager.saveUserInfo(memberEntity);
                    }
                }
                break;
            case SEX_RESULT:
                //更新性别
                if (data.hasExtra(SEX_TEXT)) {
                    userSexTv.setText("2".equals(data.getStringExtra(SEX_TEXT)) ? "女" : "男");
                }
                break;
            case AUTOGRAPH_RESULT:
                if (data.hasExtra(AUTOGRAPH_TEXT)) {
                    userAutoGraphTv.setText(data.getStringExtra(AUTOGRAPH_TEXT));
                }
                break;
        }
    }

    @Override
    public void setQiniuToken(final String token) {
        if (TextUtils.isEmpty(token)) {
            ToastUtil.showCentertoast("七牛token为空");
            return;
        }
        //重新编码并设置
        this.token = token;
    }

    @Override
    public void updateAvatar(String imgPath) {
        if (TextUtils.isEmpty(imgPath)) {
            return;
        }
        this.imgPath = imgPath;
        GlideUtil.loadCircleImage(mContext, imgPath, userLogoIv);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home: {
                //检查键盘如果打开则关闭
                SoftInputUtil.hideKeyboard((BaseActivity) mContext);

                //发送eventbus事件 更新我的页面内容
                MyUserInfoEntity myUserInfoEntity = new MyUserInfoEntity();
                MyUserInfoEntity.ObjBean objBean = new MyUserInfoEntity.ObjBean();
                myUserInfoEntity.setObj(objBean);
                objBean.setNickName(trimTextView(nickNameTv));
                objBean.setAvatar(imgPath);
                objBean.setUserId(Integer.parseInt(trimTextView(userAccountTv)));
                EventBus.getDefault().post(myUserInfoEntity);

                finish();
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
    }
}
